﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace JPR.TunelProxy.RequestGenerator.Model
{
	/// <summary>
	/// 
	/// </summary>
	internal class MainWindowModel : IDisposable, IDataErrorInfo
	{

		/// <summary>
		/// 
		/// </summary>
		internal MainWindowModel()
		{
			CommandList = new List<HttpCommand>();
			Command = HttpCommand.GET;
// ReSharper disable UseObjectOrCollectionInitializer
			Headers = new Dictionary<string, string>();
// ReSharper restore UseObjectOrCollectionInitializer
			Headers.Add("Proxy-Force", "True");
			Headers.Add("User-Agent", "TunelProxy");
			Headers.Add("Accept-Charset", "*/*");
		}

		/// <summary>
		/// 
		/// </summary>
		public void Dispose()
		{

		}

		#region Public properties

		public HttpCommand Command { get; set; }

		public string Address { get; set; }
	
		public IList<HttpCommand> CommandList { get; set; }

		public IDictionary<string, string> Headers { get; set; }

		#endregion

		#region Implementation of IDataErrorInfo

		/// <summary>
		/// Gets the error message for the property with the given name.
		/// </summary>
		/// <returns>
		/// The error message for the property. The default is an empty string ("").
		/// </returns>
		/// <param name="propertyName">The name of the property whose error message to get. </param>
		public string this[string propertyName]
		{
			get { return GetValidationError(propertyName); }
		}

		private string GetValidationError(string propertyName)
		{
			string error;
			switch (propertyName)
			{
				case "Address":
					error = ValidateAddress();
					break;
				default:
					error = null;
					break;
			}
			return error;
		}

		private string ValidateAddress()
		{
			if(string.IsNullOrWhiteSpace(Address))
			{
				return "Address has to be filled";
			}
			Regex regex=new Regex(@"^(https?://)?(?<address>[^:/]+):?(?<port>\d*)(?<objectName>/?.*)$");
			return !regex.IsMatch(Address) ? "Bad format of address" : null;
		}

		/// <summary>
		/// Gets an error message indicating what is wrong with this object.
		/// </summary>
		/// <returns>
		/// An error message indicating what is wrong with this object. The default is an empty string ("").
		/// </returns>
		public string Error
		{
			get { throw new NotImplementedException(); }
		}

		#endregion
	}
}
